package lang;

import org.junit.Test;

import static org.junit.Assert.*;

/**
 * @Author tuyou_cd
 * @Date 2020/8/4 12:11
 */
public class IntegerTest {

    /**
     * 测试常量池
     */
    @Test
    public void testIntegerPool() {

        Integer i1 = 127; //相当于调用Integer.valueOf()
        Integer i2 = 127;
        assertTrue(i1 == i2);
        assertTrue(i1.equals(i2));

        i1 = 999;
        i2 = 999;
        assertFalse(i1 == i2);
        assertTrue(i1.equals(i2));

        i1 = 127;
        i2 = Integer.valueOf(127); //Integer.valueOf会先从常量池里面取缓存，不在常量池范围才new Integer()
        assertTrue(i1 == i2);
        assertTrue(i1.equals(i2));

        i1 = new Integer(127); //new Integer不会从常量池里面取缓存
        i2 = new Integer(127);
        assertFalse(i1 == i2);
        assertTrue(i1.equals(i2));
    }
}
